package online.longzipeng.back.notify;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @Author: lzp
 * @Description: 服务器处理
 * @Date: Created in 9:56 2022/6/6
 */
public class NettyServerHandlerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        // 获取管道 pipeline
        ChannelPipeline pipeline = channel.pipeline();
        // webSocket 基于 http协议的编解码器
        pipeline.addLast(new HttpServerCodec());
        // 对数据流的写提供支持
        pipeline.addLast(new ChunkedWriteHandler());
        // 对 httpMessage 进行聚合处理 ，聚合成request或response
        pipeline.addLast(new HttpObjectAggregator(1024 * 64));

        // 前端对应的websocket url=> new WebSocket("ws:192.168.0.40:8088/ws");
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));

        // 处理获取的数据
        pipeline.addLast(new ClientDataHandler());
    }
}
